#include <QDebug>
#include <QSqlError>
#include "databaseoperations.h"
#include "databasecfg.h"
#include "logdt/logdt.h"

DatabaseOperations::DatabaseOperations() {}

DatabaseOperations::~DatabaseOperations()
{

}

DatabaseOperations &DatabaseOperations::Instance()
{
    static DatabaseOperations sData;

    return sData;
}

bool DatabaseOperations::DatabaseConnect()
{
    db = QSqlDatabase::addDatabase(DRIVER_TYPE);
    db.setHostName(HOST_NAME); // 本地数据库 远程DB是：ipaddress
    db.setPort(PORT); // 设置端口号
    db.setDatabaseName(DATABASE_NAME); // 使用的数据库 sql = use '数据库名'
    db.setUserName(DATABASE_USER_NAME);
    db.setPassword(DATABASE_PASSWORD);


    if (db.open() == true)
    {
        Logdt::LogdtInput(QString("%1数据库打开成功").arg(DATABASE_NAME));
    }
    else
    {
        Logdt::LogdtInput(QString("%1数据库打开失败").arg(DATABASE_NAME));
    }

    return db.isOpen();
}
